package com.ymatou.iapi.pushbind.testcase;

import com.ymatou.iapi.pushbind.parameter.BindUserClientBean;
import com.ymatou.iapi.pushbind.parameter.Enum.AppIdType;
import com.ymatou.iapi.pushbind.parameter.Enum.PhoneType;
import com.ymatou.iapi.pushbind.parameter.Enum.UserSystemType;
import com.ymatou.iapi.pushbind.service.BindUserClientCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
*绑定用户客户端
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_BindUserClient{
	private static BindUserClientBean binduserclientBean;
	private static BindUserClientCall binduserclientCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定用户客户端");
	}

	@Before
	public void caseUp() {
		binduserclientBean=new BindUserClientBean();
		binduserclientCall=new BindUserClientCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(binduserclientCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindUserClient_001() {
		Logger.start(true,"绑定用户客户端,YmatouSellerCsUser用户");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.YmatouSellerCsUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
//			Integer userId = urrc.getUserid();
			Integer userId = 1;
			binduserclientBean.setUserId(userId);
			String deviceId = "xiaomage"+YMTDateUtil.getBeforeOrNextSecond(1);
			binduserclientBean.setDeviceId(deviceId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();			
			Logger.verifyEquals("200",binduserclientCall.getString("Status"),"验证Code");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBind(binduserclientBean.getClientId()).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(sqlMap.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(deviceId,sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getBindUserRedis("BindRecord:UserBindSet:2:"+userId);
			Logger.debug(set);
			for (String s:set
				 ) {
				JSONObject json = FormatUtil.JSONToJSONObject(s);
				Logger.verifyEquals(json.getInt("ui"),binduserclientBean.getUserId(),"验证Redis中包含有UserId");
				Logger.verifyEquals(json.getString("ci"),binduserclientBean.getClientId(),"验证Redis中包含有clientId");
				Logger.verifyEquals(json.getString("di"),binduserclientBean.getDeviceId(),"验证Redis中包含有deviceId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindUserClient_002() {
		Logger.start(true,"绑定用户客户端,YmatouUser用户");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.YmatouUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			binduserclientBean.setUserId(userId);
			String deviceId = "xiaomage"+YMTDateUtil.getBeforeOrNextSecond(1);
			binduserclientBean.setDeviceId(deviceId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();
			Logger.verifyEquals("200",binduserclientCall.getString("Status"),"验证Code");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBind(binduserclientBean.getClientId()).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(sqlMap.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(deviceId,sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getBindUserRedis("BindRecord:UserBindSet:"+userId);
			Logger.debug(set);
			for (String s:set
			) {
				JSONObject json = FormatUtil.JSONToJSONObject(s);
				Logger.verifyEquals(json.getInt("ui"),binduserclientBean.getUserId(),"验证Redis中包含有UserId");
				Logger.verifyEquals(json.getString("ci"),binduserclientBean.getClientId(),"验证Redis中包含有clientId");
				Logger.verifyEquals(json.getString("di"),binduserclientBean.getDeviceId(),"验证Redis中包含有deviceId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindUserClient_003() {
		Logger.start(true,"绑定用户客户端,CsUser用户");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.CsUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
//			Integer userId = urrc.getUserid();
			Integer userId = 1;
			binduserclientBean.setUserId(userId);
			String deviceId = "xiaomage"+YMTDateUtil.getBeforeOrNextSecond(1);
			binduserclientBean.setDeviceId(deviceId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();
			Logger.verifyEquals("200",binduserclientCall.getString("Status"),"验证Code");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBind(binduserclientBean.getClientId()).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(sqlMap.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(deviceId,sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getBindUserRedis("BindRecord:UserBindSet:1:"+userId);
			String str = StringUtils.join(set.toArray(), ";");
			for (String s:set
			) {
				JSONObject json = FormatUtil.JSONToJSONObject(s);
				Logger.verifyEquals(json.getInt("ui"),binduserclientBean.getUserId(),"验证Redis中包含有UserId");
				Logger.verifyEquals(json.getString("ci"),binduserclientBean.getClientId(),"验证Redis中包含有clientId");
				Logger.verifyEquals(json.getString("di"),binduserclientBean.getDeviceId(),"验证Redis中包含有deviceId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
		@Test
		@Category({P1.class})
		@TestCase
		@Deprecated
		public void Tc_BindUserClient_004() {
		Logger.start(true,"绑定用户客户端,userId不为空,DeviceId为空");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.YmatouUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			binduserclientBean.setUserId(userId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();
			Logger.verifyEquals("500",binduserclientCall.getString("Status"),"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindUserClient_005() {
		Logger.start(true,"绑定用户客户端,userid为空，deviceId不为空");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.YmatouUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			String deviceId = "xiaomage"+YMTDateUtil.getBeforeOrNextSecond(1);
			binduserclientBean.setDeviceId(deviceId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();
			Logger.verifyEquals("200",binduserclientCall.getString("Status"),"验证Code");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBind(binduserclientBean.getClientId()).get(0);
			Logger.verifyEquals(deviceId,sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			/*Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			int id = getStringHashCode(deviceId);
			Logger.debug(id);
			Set<String> set= im.getZrange(id,"BindRecord:DeviceBindSet:BuyerLive:"+id);
			Logger.debug(set);
			String str = StringUtils.join(set.toArray(), ";");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getUserId().toString()),"验证Redis中包含有UserId");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getClientId().toString()),"验证Redis中包含有clientId");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getDeviceId().toString()),"验证Redis中包含有deviceId");
*/
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BindUserClient_006() {
		Logger.start(true,"绑定用户客户端,推手,YmatouUser用户,绑定过的devicid");
		try {
			String appId= "pushbind.iapi.com";
			binduserclientBean.setAppId(appId);
			binduserclientBean.setUserSystemType(UserSystemType.YmatouUser);
			binduserclientBean.setAppIdType(AppIdType.ShenqiBuyer);
			binduserclientBean.setPhoneType(PhoneType.iOS);
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			binduserclientBean.setUserId(userId);
			String deviceId = "xiaomage"+YMTDateUtil.getBeforeOrNextSecond(1);
			binduserclientBean.setDeviceId(deviceId);
			String clientId = "Test" + YMTDateUtil.getDate();
			binduserclientBean.setClientId(clientId);
			String deviceIdToken = "Test123" + YMTDateUtil.getDate();
			binduserclientBean.setDeviceToken(deviceIdToken);
			binduserclientCall.setData(binduserclientBean);
			binduserclientCall.callService();
			Logger.verifyEquals("200",binduserclientCall.getString("Status"),"验证Code");
			Logger.comment("验证数据库的数据");
			ImDefaultWapper imDefaultWapper = new ImDefaultWapper();
			Map sqlMap = imDefaultWapper.selectPushBind(binduserclientBean.getClientId()).get(0);
			Logger.debug(sqlMap);
			Logger.verifyEquals(userId,Integer.parseInt(sqlMap.get("user_id").toString()),"验证userId");
			Logger.verifyEquals(deviceId,sqlMap.get("device_id").toString(),"验证deviceId");
			Logger.verifyEquals(clientId,sqlMap.get("client_id").toString(),"验证Client");
			Logger.comment("验证redis数据");
			ImRedisWapper im = new ImRedisWapper();
			Set<String> set= im.getBindUserRedis("BindRecord:UserBindSet:"+userId);
			Logger.debug(set);
			String str = StringUtils.join(set.toArray(), ";");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getUserId().toString()),"验证Redis中包含有UserId");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getClientId().toString()),"验证Redis中包含有clientId");
			Logger.verifyEquals(true,str.contains(binduserclientBean.getDeviceId().toString()),"验证Redis中包含有deviceId");
			Thread.sleep(5000);
			int userid1 = new Random().nextInt(10000);
			binduserclientBean.setUserId(userid1);
			binduserclientCall.callService();
			sqlMap = imDefaultWapper.selectPushBindBydeciveId(deviceId).get(0);
			Logger.debug(sqlMap);
			Logger.comment("验证redis数据");
			Set<String> set1= im.getBindUserRedis("BindRecord:UserBindSet:"+userid1);
			Logger.debug(set1);
			String str1 = StringUtils.join(set1.toArray(), ";");
			//Logger.verifyEquals(false,str1.isEmpty(),"验证redis数据为空");
//			Logger.verifyEquals(true,str1.contains(binduserclientBean.getUserId().toString()),"验证Redis中包含有UserId");
//			Logger.verifyEquals(true,str1.contains(binduserclientBean.getClientId().toString()),"验证Redis中包含有clientId");
//			Logger.verifyEquals(true,str1.contains(binduserclientBean.getDeviceId().toString()),"验证Redis中包含有deviceId");
//
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static int getStringHashCode(String s) {
		if (org.apache.commons.lang.StringUtils.isEmpty(s)) {
			return 0;
		}
		char lastChar = s.charAt(s.length() - 1);
		return lastChar;
	}
}